<?php
/**
 * cms后台幻灯图管理页
 * author wang
 */

define("PHP_IN",true);
require "../include/init.php";
require "checklogin.php";
require ROOT_PATH."cms/smart_connection.php";

$page = isset($_GET['page']) ? $_GET['page'] : 1 ;
$pagesize = 10;
$pagestart = ($page-1) * $pagesize ;

$sql = "SELECT count(*) AS num FROM slidead";
$result = $db->query($sql);
$row = $db->fetch_one($result);
$count = $row['num'];

$pagecount = ceil($count/$pagesize);
$page_links = cms_page_links($pagecount, $page, "slide_list.php?$page");

$sql = "SELECT * FROM slidead ORDER BY adid DESC LIMIT $pagestart,$pagesize ";
$result = $db->query($sql);
$slide = $db->fetch_all($result);

if(isset($_GET['act']) && $_GET['act']=="del"){
    $deleteid = isset($_GET['deleteid']) ? $_GET['deleteid'] : 0;
    $page = isset($_GET['page'])?intval($_GET['page']):1;
    $pagecount = isset($_GET['pagecount'])?intval($_GET['pagecount']):0;
    $pagesize = isset($_GET['pagesize'])?intval($_GET['pagesize']):9;

    $sql = "DELETE FROM slidead WHERE adid = $deleteid";
    $result = $db->query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM slidead";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }

        header("Location:slide_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

if(isset($_GET['act']) && $_GET['act']=="delall" && !empty($_POST['idarr'])){
    $deleteid = isset($_POST['idarr']) ? implode(",",$_POST['idarr']) : "";

    $sql = "DELETE FROM slidead WHERE adid IN ($deleteid)";
    $result = $db->query("$sql");
    if($result){
        $sql = "SELECT count(*) AS num FROM slidead";
        $result = $db->query($sql);
        $row = $db->fetch_one($result);
        $count = $row['num'];

        $pagecount_now = ceil($count/$pagesize);
        if($page==1){
            $page==1;
        }else{
            if($page == $pagecount){
                if($pagecount_now < $pagecount){
                    $page = $pagecount_now;
                }
            }
        }

        header("Location:slide_list.php?page=".$page);
    }else{
        alert("删除失败");
    }
}

$smarty->assign("slide",$slide);
$smarty->assign("page",$page);
$smarty->assign("pagecount",$pagecount);
$smarty->assign("pagesize",$pagesize);
$smarty->assign("page_links",$page_links);

$smarty->display("slide_list.tpl");
$db->close();
?>
